package com.jtny.nytb.config.measurementtype.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.WebResponse;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.config.excel.biz.ImportExcelBiz;
import com.jtny.nytb.config.excel.util.EasyExcelUtils;
import com.jtny.nytb.config.measurementtype.service.MeasurementTypeService;
import com.jtny.nytb.config.measurementtype.vo.ExcelMeasurementTypeVO;
import com.jtny.nytb.config.measurementtype.vo.MeasurementTypePageQueryParamVO;
import com.jtny.nytb.config.measurementtype.vo.MeasurementTypeVO;
import com.jtny.nytb.config.measurementunit.vo.ExcelMeasurementUnitVO;
import com.jtny.nytb.config.varible.vo.ExcelGlobalParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 计量单位分类Controller类
 * @package com.jtny.nytb.config.measurementType.controller
 * @fileName MeasurementTypeController.java
 * @author 智慧能源中心-lyq
 * @createTime 2023年09月01日 11:15
 * @version 1.0
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@RestController
@Api(value = "计量单位分类管理", tags = "计量单位分类管理")
@RequestMapping("/config/measurementType")
@AllArgsConstructor
public class MeasurementTypeController {
    @Resource
    private MeasurementTypeService measurementTypeService;
    private ImportExcelBiz importExcelBiz;

    private static final String OPER_SUCCESS_MESSAGE = "操作成功！";

    @ApiOperation("计量单位分类新增操作")
    @ApiImplicitParam(name = "measurementTypeVO", value = "计量单位分类新增操作", required = true, paramType = "query", dataType = "MeasurementTypeVO")
    @ApiOperationSupport(order = 1)
    @PostMapping("/save")
    @ResponseBody
    public WebResponse<?> saveMeasurementType(@RequestBody @Valid MeasurementTypeVO measurementTypeVO) {
        try{
            MeasurementTypeVO measurementType = measurementTypeService.saveMeasurementType(measurementTypeVO);
            return WebResponse.success(OPER_SUCCESS_MESSAGE, measurementType);
        }catch (ServiceException e){
            return WebResponse.error(e);
        }
    }

    @ApiOperation("计量单位分类更新操作")
    @ApiImplicitParam(name = "measurementTypeVO", value = "计量单位分类更新操作", required = true, paramType = "query", dataType = "MeasurementTypeVO")
    @ApiOperationSupport(order = 2)
    @PutMapping("/update")
    @ResponseBody
    public WebResponse<?> updateMeasurementType(@RequestBody @Valid MeasurementTypeVO measurementTypeVO) {
        try{
            measurementTypeService.updateMeasurementType(measurementTypeVO);
            return WebResponse.success(OPER_SUCCESS_MESSAGE);
        }catch (ServiceException e){
            return WebResponse.error(e);
        }
    }

    @ApiOperation("计量单位分类查看操作")
    @ApiImplicitParam(name = "measurementTypeId", value = "计量单位分类id", required = true, paramType = "path")
    @ApiOperationSupport(order = 3)
    @GetMapping("/detail/{measurementTypeId}")
    @ResponseBody
    public WebResponse<?> detailMeasurementType(@PathVariable("measurementTypeId") Long measurementTypeId) {
        MeasurementTypeVO measurementTypeVO = measurementTypeService.detailMeasurementType(measurementTypeId);
        return WebResponse.success(measurementTypeVO);
    }

    @ApiOperation("计量单位分类分页查询操作")
    @ApiImplicitParam(name = "measurementTypePageQueryParamVO", value = "计量单位分类分页查询操作", required = true, paramType = "query", dataType = "MeasurementTypePageQueryParamVO")
    @ApiOperationSupport(order = 4)
    @PostMapping("/queryPage")
    @ResponseBody
    public WebResponse<?> pageQuery(@RequestBody @Valid MeasurementTypePageQueryParamVO measurementTypePageQueryParamVO) {
        PageData<MeasurementTypeVO> measurementTypeVOPageData = measurementTypeService.queryByPage(measurementTypePageQueryParamVO);
        return WebResponse.success(measurementTypeVOPageData);
    }

    /**
     * 删除计量单位分类
     * @param measurementTypeId 计量单位分类Id
     * @throws ServiceException 抛出异常
     */
    @ApiOperation("删除计量单位分类")
    @ApiImplicitParam(name = "measurementTypeId", value = "计量单位分类id", required = true, paramType = "path")
    @ApiOperationSupport(order = 5)
    @GetMapping("/delete/{measurementTypeId}")
    @ResponseBody
    public WebResponse<?> deleteMeasurementType(@PathVariable("measurementTypeId") Long measurementTypeId){
        measurementTypeService.deleteMeasurementType(measurementTypeId);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }

    /**
     * 启用计量单位分类
     * @param measurementTypeId 计量单位分类Id
     * @throws ServiceException 抛出异常
     */
    @ApiOperation("启用计量单位分类")
    @ApiImplicitParam(name = "measurementTypeId", value = "计量单位分类id", required = true, paramType = "path")
    @ApiOperationSupport(order = 6)
    @GetMapping("/enable/{measurementTypeId}")
    @ResponseBody
    public WebResponse<?> enableMeasurementType(@PathVariable("measurementTypeId") Long measurementTypeId){
        measurementTypeService.enableMeasurementType(measurementTypeId);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }

    /**
     * 禁用计量单位分类
     * @param measurementTypeId 计量单位分类Id
     * @throws ServiceException 抛出异常
     */
    @ApiOperation("禁用计量单位分类")
    @ApiImplicitParam(name = "measurementTypeId", value = "计量单位分类id", required = true, paramType = "path")
    @ApiOperationSupport(order = 7)
    @GetMapping("/disable/{measurementTypeId}")
    @ResponseBody
    public WebResponse<?> disableMeasurementType(@PathVariable("measurementTypeId") Long measurementTypeId){
        measurementTypeService.disableMeasurementType(measurementTypeId);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }


    @ApiOperation("获取所有计量单位分类的信息")
    @ApiOperationSupport(order = 8)
    @PostMapping("/queryAll")
    @ResponseBody
    public WebResponse<?> queryAll() {
        List<MeasurementTypeVO> measurementTypeVOS = measurementTypeService.queryAll();
        return WebResponse.success(measurementTypeVOS);
    }


    /**
     * 获取导入模板
     * @param response 响应
     */
    @PostMapping("/export-template")
    @ApiOperation(value = "获取导入模板")
    public void exportTemplate(HttpServletResponse response) {
        EasyExcelUtils.exportExcel(response, "计量单位导入模版", null, ExcelGlobalParamVO.class);
    }

    @ApiOperation("导入计量单位")
    @ApiImplicitParam(name = "file", value = "计量单位excel文件", required = true, paramType = "query", dataType = "MultipartFile")
    @ApiOperationSupport(order = 9)
    @PostMapping("/import")
    @ResponseBody
    public WebResponse<?> importMeasurementUnit(@RequestParam(value = "file") MultipartFile file) {
        List<ExcelMeasurementUnitVO> list = EasyExcelUtils.importExcel(file, ExcelMeasurementUnitVO.class);
        importExcelBiz.importMeasurementUnitExcel(list);
        return WebResponse.success();
    }

    @ApiOperation("导出计量单位分类")
    @ApiOperationSupport(order = 10)
    @PostMapping("/export")
    @ApiImplicitParam(name = "idList", value = "id列表", required = true, paramType = "body", dataType = "List<Long>")
    @ResponseBody
    public void exportMeasurementUnit(HttpServletResponse response, @RequestBody List<Long> idList) {
        MeasurementTypePageQueryParamVO paramVO = new MeasurementTypePageQueryParamVO();
        paramVO.setIdList(idList);
        paramVO.setPageSize(10000);
        PageData<MeasurementTypeVO> page = measurementTypeService.queryByPage(paramVO);
        List<ExcelMeasurementTypeVO> toList = BeanUtils.copyToList(page.getData(), ExcelMeasurementTypeVO.class);
        EasyExcelUtils.exportExcel(response, "数据字典", toList, ExcelMeasurementTypeVO.class);
    }
}

